/**
 * Definition for a binary tree node.
 * type TreeNode struct {
 *     Val int
 *     Left *TreeNode
 *     Right *TreeNode
 * }
 */
func isBalanced(root *TreeNode) bool {
    if root == nil {
        return true
    }
    if !isBalanced(root.Left) || !isBalanced(root.Right){
       return false 
    }
    leftH := maxDepth(root.Left)
    RightH := maxDepth(root.Right)
    if abs(leftH-RightH) > 1 {
        return false
    }
    return true
}

func maxDepth(node *TreeNode) int {
    if node == nil {
        return 0
    }
    LeftD := maxDepth(node.Left)
    RightD := maxDepth(node.Right)
    return max(LeftD, RightD) + 1
}

func max(a, b int) int {
    if a>b{
        return a
    }
    return b
}

func abs(a int) int {
    if a < 0 {
        return -a
    }
    return a
}